package pkg

import (
	"encoding/json"
	"fmt"
	"log"
	"os"

	"gitee.com/chuanjinge/go-container/container/containerconfig"
	"gitee.com/chuanjinge/go-container/pkg/loggers"
)

func LogConf(value any) {
	cInfoByte, _ := json.Marshal(value)
	//log.Println(string(cInfoByte))
	loggers.GetLogConf().LoggerConf.Print(string(cInfoByte))
}

func InitLogConf(containerId string) {
	// 初始化配置信息
	err := loggers.InitializeLoggerConf(containerconfig.GetConfigLog(containerId))
	if err != nil {
		log.Printf("容器运行时信息日志初始化日常\n")
		os.Exit(1)
	}
}

func CloseLogConf() {
	loggers.CloseLoggerConf()
}

func InitLoggers(containerId string) {
	logRunPath := fmt.Sprintf("/data/tinydocker/container/%s/%s-common.log", containerId, containerId)
	config := loggers.Config{
		RunFilePath: logRunPath,
	}

	err := loggers.InitializeLoggers(config)
	if err != nil {
		log.Fatalf("[%s][initLoggers]failed to initialize loggers: %v", containerId, err)
	}

	loggersInstance := loggers.GetLoggers()
	if loggersInstance == nil {
		log.Fatalf("[%s][initLoggers]failed to get loggers", containerId)
	}
}
